package xyz.liut.onmyojihelper.runnable

import android.os.Handler
import android.os.Message
import android.util.Log
import eu.chainfire.libsuperuser.Shell
import xyz.liut.onmyojihelper.BuildConfig
import xyz.liut.onmyojihelper.CoreService
import xyz.liut.onmyojihelper.util.LogUtil
import java.util.*

/**
 * OnmyojiHelper
 * 模拟点击屏幕
 * </p>
 * Created by liut on 2017/11/2 0002.
 */
class ClickRunnable(private var handler: Handler,
                    var roundNum: Int,
                    private val roundTime: Double,
                    private val intervalTime: Double) : Runnable {

    companion object {
        val TAG = "ClickRunnable"
    }

    var isRunning = false

    var roundTimeFinal: Double = 0.toDouble()
    var intervalTimeFinal: Double = 0.toDouble()
    var tmpRoundTime: Double = 0.toDouble()
    var tmpIntervalTime: Double = 0.toDouble()

    override fun run() {

        LogUtil.d(TAG, " time : $roundTime  $intervalTime")

        try {
            isRunning = true

            while (roundNum > 0) {
                roundTimeFinal = roundTime + Random().nextInt(500) / 100
                intervalTimeFinal = intervalTime + Random().nextInt(300) / 100
                tmpRoundTime = roundTimeFinal
                tmpIntervalTime = intervalTimeFinal

                start()

                while (tmpRoundTime >= 0) {
                    val random = Random().nextInt(800)
                    LogUtil.d(TAG, random.toLong().toString())
                    Thread.sleep(random.toLong())
                    tmpRoundTime -= random.toDouble() / 1000

                    LogUtil.d(TAG, "tmpTime0 >> " +
                            random.toDouble() / 1000 +
                            "----" +
                            tmpRoundTime.toString())
                }

                // todo 时间调整

                end()

                while (tmpIntervalTime > 0) {
                    val random = Random().nextInt(800)
                    Thread.sleep(random.toLong())
                    tmpIntervalTime -= random.toDouble() / 1000
                }

                roundNum--

                LogUtil.d(TAG, "msg -- >> $tmpRoundTime $tmpIntervalTime $roundNum")
            }

        } catch (e: Exception) {
            Log.e("liut", e.message + "--" + e.printStackTrace())
        } finally {
            isRunning = false
        }


    }

    private fun start() {
        click(BuildConfig.START_X, BuildConfig.START_Y)
        val msg = Message()
        msg.what = CoreService.STATE_SHOW
        msg.arg1 = roundNum
        handler.sendMessage(msg)
    }

    private fun end() {
        click(BuildConfig.END_X, BuildConfig.END_Y)
        val msg = Message()
        msg.what = CoreService.STATE_END
        msg.arg1 = roundNum
        handler.sendMessage(msg)
    }

    private fun click(x: Int, y: Int) {
        click(x, y, 40)
    }

    private fun click(x: Int, y: Int, randomNum: Int) {
        val x2 = x - randomNum / 2 + Random().nextInt(randomNum)
        val y2 = y - randomNum / 2 + Random().nextInt(randomNum)
        val time = randomNum / 2 + Random().nextInt(randomNum)
        LogUtil.d(TAG, "x: $x y: $y random: $randomNum")
        LogUtil.d(TAG, "x: $x2 y: $y2")
        Shell.SU.run("input swipe $x2 $y2 $x2 $y2 $time")
    }

}